package com.frank.loadinglibrary.common;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * @author SUQI
 * @date 2021/5/13
 * @description WiFi loading类型
 **/
public class WifiLoadingComponent extends BaseLoadingComponent<WifiLoadingComponent> implements Component.DrawTask {

    private int num = 3;
    private float baseRadius;
    private Paint mPaint;
    private float broad;
    private float strokeWidth;
    private Color[] colors = new Color[]{new Color(Color.getIntColor("#abbd81")), new Color(Color.getIntColor("#f8b26a")), new Color(Color.getIntColor("#f47e60"))};
    private float currentValue;
    private AnimatorValue animatorValue;

    public WifiLoadingComponent(Context context) {
        this(context, null);
    }

    public WifiLoadingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public WifiLoadingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initPaint();
        addDrawTask(this);
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                startAnimators();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.MAGENTA);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void stopAnimators() {
        if (animatorValue != null) {
            animatorValue.cancel();
            animatorValue.end();
        }
    }

    @Override
    protected void startAnimators() {
        animatorValue = new AnimatorValue();
        animatorValue.setLoopedCount(Animator.INFINITE);
        animatorValue.setDuration(1000);
        animatorValue.setDelay(1000);
        animatorValue.setValueUpdateListener((animatorValue1, v) -> {
            currentValue = v;
            invalidate();
        });
        animatorValue.start();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.save();
        canvas.translate(0, baseRadius);
        broad = Math.min(getWidth(), getHeight());
        baseRadius = broad / 2f / num;
        strokeWidth = baseRadius / 2f;
        for (int i = 0; i < num; i++) {
            mPaint.setColor(colors[i]);
            float radius = baseRadius * i;
            RectFloat rect = new RectFloat(radius + strokeWidth, radius + strokeWidth, broad - radius - strokeWidth, broad - radius - strokeWidth);
            HiLog.debug(new HiLogLabel(HiLog.DEBUG, 1, "sss"), "index: " + currentValue * (num));
            if (i >= num - (int) (currentValue * (num + 1))) {
                if (i == num - 1) {
                    mPaint.setStyle(Paint.Style.FILL_STYLE);
                    mPaint.setStrokeWidth(0);
                    canvas.drawArc(rect, new Arc(0, 360, true), mPaint);
                } else {
                    mPaint.setStyle(Paint.Style.STROKE_STYLE);
                    mPaint.setStrokeWidth(strokeWidth);
                    canvas.drawArc(rect, new Arc(-90, 90, false), mPaint);
                }
            }
        }
        canvas.restore();
    }
}
